<?php
/**
 * Created by PhpStorm.
 * User: whwyy
 * Date: 2018/4/4 0004
 * Time: 15:47
 */

namespace Database;

/**
 * Class HasBase
 * @package Database
 *
 * @include TraitModel
 */
abstract class HasBase
{
	
	/** @var ActiveRecord|Collection */
	protected $data;
	
	/** @var Model */
	protected $model;
	
	/** @var array */
	protected $collect;
	
	/** @var ActiveRecord */
	protected $oldModel;
	
	protected $primaryId;
	
	protected $forgetKey;
	
	protected $values = [];
	
	/**
	 * HasBase constructor.
	 * @param ActiveRecord $model
	 * @param $primaryId
	 * @param string $forgetKey
	 * @param ActiveRecord $oldModel
	 * @throws \Exception
	 */
	public function __construct($model, $primaryId, $forgetKey = 'id', $value)
	{
		$this->values[] = $value;
		$this->primaryId = $primaryId;
		$this->forgetKey = $forgetKey;
		$this->collect[$value] = [$primaryId, $forgetKey];
		
		//$primaryId
		$this->model = $model::find()->where([$primaryId => $value]);
	}
	
	/**
	 * @return mixed|Model
	 */
	public function getModel()
	{
		return $this->model;
	}
	
	/**
	 * @return mixed|Model
	 */
	public function getPrimaryKey()
	{
		return $this->primaryId;
	}
	
	/**
	 * @return mixed|Model
	 */
	public function getForgetKey()
	{
		return $this->forgetKey;
	}
	
	/**
	 * @return mixed
	 */
	public function getValue()
	{
		return $this->value;
	}
	
	abstract public function get();
	
	/**
	 * @param $name
	 * @return mixed
	 */
	public function __get($name)
	{
		return $this->get();
	}
}